<template>
    <label class="switch">
        <input type="checkbox" v-model="checked">
        <span class="slider"></span>
    </label>
</template>

<style lang="scss" scoped>
.switch {
    position: relative;
    display: inline-block;
    width: 52px;
    height: 30px;
}

.switch input {
    display: none;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgb(175, 175, 175);
    -webkit-transition: .4s;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 4px;
    bottom: 4px;
    background-color: rgb(255, 255, 255);
    -webkit-transition: .4s;
    transition: .4s;
}

input:checked+.slider {
    background-color: rgb(15, 75, 230);
}

input:checked+.slider:before {
    -webkit-transform: translateX(22px);
    transform: translateX(22px);
}
</style>

<script>
export default {
    props: ['initiate'],
    data() {
        return {
            checked: false
        };
    },
    mounted() {
        this.checked = this.initiate;
    },
    watch: {
        initiate(newValue) {
            this.checked = newValue;
        },
        checked(newValue) {
            this.$emit('switch-changed', newValue);
        }
    }
}
</script>